package com.xyzboom.compiler.compiler.semantic.binds

import javax.script.SimpleBindings

val anyOperatorsBinding = SimpleBindings().apply {
    put("plus", ::plus)
    put("minus", ::minus)
    put("times", ::times)
    put("div", ::div)
}

operator fun Any?.plus(b: Any?): Any? {
    return when (this) {
        is Int -> when (b) {
            is Int -> this + b
            is Float -> this + b
            else -> null
        }

        is Float -> when (b) {
            is Int -> this + b
            is Float -> this + b
            else -> null
        }

        else -> null
    }
}

operator fun Any?.minus(b: Any?): Any? {
    return when (this) {
        is Int -> when (b) {
            is Int -> this - b
            is Float -> this - b
            else -> null
        }

        is Float -> when (b) {
            is Int -> this - b
            is Float -> this - b
            else -> null
        }

        else -> null
    }
}

operator fun Any?.times(b: Any?): Any? {
    return when (this) {
        is Int -> when (b) {
            is Int -> this * b
            is Float -> this * b
            else -> null
        }

        is Float -> when (b) {
            is Int -> this * b
            is Float -> this * b
            else -> null
        }

        else -> null
    }
}

operator fun Any?.div(b: Any?): Any? {
    return when (this) {
        is Int -> when (b) {
            is Int -> this / b
            is Float -> this / b
            else -> null
        }

        is Float -> when (b) {
            is Int -> this / b
            is Float -> this / b
            else -> null
        }

        else -> null
    }
}